package com.example.lib.extend

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.ColorRes
import androidx.annotation.StringRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.example.lib.R
import com.example.lib.databinding.EmptyLayoutBinding


fun Fragment.getResourceColor(@ColorRes color: Int): Int {
    return ContextCompat.getColor(requireContext(), color)
}

fun Fragment.getResourceString(@StringRes str: Int): String {
    //兼容Fragment not attached to a context 问题
    return context?.resources?.getString(str) ?: ""
}

fun Fragment.getArgumentString(key: String, default: String = ""): String {
    return arguments?.let {
        it.getString(key) ?: return default
    } ?: let { default }

}

fun Fragment.getArgumentInt(key: String, default: Int = 0): Int {
    return arguments?.getInt(key) ?: let { default }
}

fun Fragment.getArgumentLong(key: String, default: Long = 0): Long {
    return arguments?.getLong(key) ?: let { default }

}

fun Fragment.getArgumentBoolean(key: String, default: Boolean = false): Boolean {
    return arguments?.getBoolean(key) ?: let { default }
}

/**
 * 缺省图显示
 * 警告，外部不要使用RelativeLayout包裹，会出现堆叠问题,
 */
fun Fragment.goneLayout(view: View,
    views: MutableList<View>,
    text: String,
    res: Int = R.mipmap.icon_empty,
    genBack: Int = 0,
    genMargin: Int = 0,
    imageWidth: Int = 0,
    imageHeight: Int = 0) {
    if (view is ViewGroup) {
        val relative = EmptyLayoutBinding.bind(LayoutInflater.from(requireContext())
            .inflate(R.layout.empty_layout, view, false) as ConstraintLayout)
        if (view.tag != null) return
        view.removeView(relative.root)
        view.addView(relative.root)
        relative.root.layoutParamsMatchParent()

        if (genBack != 0) relative.root.setBackgroundResource(genBack)

        if (genMargin > 0) {
            val margin = requireContext().dip2px(genMargin.toFloat())
            relative.root.setMargin(margin, margin, margin, margin)
        }

        if (imageWidth != 0 && imageHeight != 0) {
            relative.EmptyLayoutImg.setWidthOrHeight(width = imageWidth, height = imageHeight)
        }

        if (res == 0) relative.EmptyLayoutImg.gone()
        else {
            relative.EmptyLayoutImg.visi()
            relative.EmptyLayoutImg.loadImage(res)
        }

        relative.EmptyLayoutText.also {
            if (text.isEmpty()) it.gone()
            else {
                it.visi()
                it.text = text
            }
        }

        views.forEach {
            it.gone()
        }
        view.tag = relative.root
    }
}

/**
 * 缺省图移除
 */
fun Fragment.visibleLayout(view: View, views: MutableList<View>) {
    if (view is ViewGroup) {
        val relative =
            LayoutInflater.from(requireContext()).inflate(R.layout.empty_layout, view, false) as ConstraintLayout
        if (view.tag == null) {
            view.removeView(relative)
        } else {
            if (view.tag != null) {
                if (view.tag is View) {
                    view.removeView(view.tag as View)
                }
            }
            view.tag = null
        }
        views.forEach {
            it.visi()
        }
    }
}

fun Fragment.dip2px(dpValue: Float): Int {
    return activity?.dip2px(dpValue) ?: 0
}



